<?php
/**
 * Created by : PhpStorm
 * User: ZPF
 * Date: 2022/3/30
 * Time: 22:53
 */

namespace app\admin\model;


use think\facade\Validate;
use think\Model;

class RoleModel extends Model
{
    protected $name='role';
    protected static $rule = [
        "role_name" => "request",
        "permission" => "request",
    ];
    /**
     * 获取角色列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getList($where=[],$page=1,$limit=10)
    {
        $list = static::where($where)->page($page,$limit)->select()->toArray();
        foreach ($list as $key=>$item){
            $list[$key]['permission']=explode('-',$item['permission']);
        }
        return $list;
    }

    /**
     * 编辑角色
     * @param array $p
     * @return array
     */
    public static function editRole($p = []){
        $p = !empty($p) ? filter_param($p) : [];   //过滤参数
        $validate =  Validate::rule(static::$rule);
        if($validate->check($p)) return ['code' => 50001 , "msg" => $validate->getError()];
        if(!empty($p['id'])){
            $res = static::update($p , ['id' => $p['id']]);
        }else{
            $res = static::create($p);
        }
        return ['code' => $res ? 200 : 50001 , "msg" => $res ? "操作成功" : "操作失败"];
    }
    public static function getInfo($id){
        return static ::find($id);
    }

    /**
     * 删除角色
     * @param $id
     * @return array
     */
    public static function del($ids){
        $res=static ::where('id','in',$ids)->delete();
        return ['code' => $res ? 200 : 50001 , "msg" => $res ? "操作成功" : "操作失败"];
    }
    public static function count($where=[])
    {
        $count=static ::where($where)->count();
        return $count;
    }


}
